import { reqCartList, reqDeleteCartById, reqUpdateCartById } from "@/api";
const state = {
    cartList: [],
};
const mutations = {
    GETCARTLIST(state, cartList) {
        state.cartList = cartList;
    }
};
const actions = {
    async getCartList({ commit }) {
        let result = await reqCartList();
        if (result.code == 200) {
            commit("GETCARTLIST", result.data);
        }
    },
    // 删除购物车信息
    async deleteCartListBySkuId({ commit }, skuId) {
        let result = await reqDeleteCartById(skuId);
        if (result.code == 200) {
            return "ok";
        } else {
            // console.log(result);
            return Promise.reject(new Error("faile"));
        }
    },
    // 修改商品状态
    async updateCheckedById({ commit }, { skuId, isChecked }) {
        let result = await reqUpdateCartById(skuId, isChecked);
        if (result.code == 200) {
            console.log(result);
            return "ok";
        } else {
            console.log(result);
            return Promise.reject(new Error("faile"));
        }
    },
    // 删除所有选中的商品
    deleteAllCheckedCart({ dispatch, getters }) {
        /* context:小仓库；commit【提交mutations修改state】
        getters【计算属性】，dispatch【派发action】，state【当前仓库】
         */
        let PromiseAll = [];
        // 获取购物车中全部的数据
        getters.cartList.cartInfoList.forEach((item) => {
            let promise = item.isChecked == 1 ? dispatch("deleteCartListBySkuId", item.skuId) : "";
            PromiseAll.push(promise);
        });
        return Promise.all(PromiseAll);
    },
    // 修改全部商品的状态
    updateAllCartIsChecked({ dispatch, state }, isChecked) {
        let promiseAll = [];
        state.cartList[0].cartInfoList.forEach(item => {
            let promise = dispatch("updateCheckedById", { skuId: item.skuId, isChecked });
        });
        return Promise.all(promiseAll);
    }
};
const getters = {
    cartList(state) {
        return state.cartList[0] || {};
    },
};
export default {
    state,
    mutations,
    actions,
    getters
}
